#include <iostream>
#include <stack>
#include <string>
#include <fstream>
using namespace std;

bool isNumber(char s) //用于判定是否是数字
{
    if (s >= '0' && s <= '9')
    {
        return true;
    }
    return false;
}

//用数字表示优先级，把‘+’和‘-’定义为1，‘*’和‘/’定义为2,数字大代表优先级大
int priorOfOperator(char operator_s)
{
    int temp = 0;
    if (operator_s == '+' || operator_s == '-')
    {
        temp = 1;
    }
    else if (operator_s == '*' || operator_s == '/')
    {
        temp = 2;
    }
    return temp;
}

/*处理运算符，遇到运算符比栈顶优先级高的，则压栈
否则替代栈顶的运算符，将栈顶运算符加入输出队列*/
void handleStackOperator(string &result, char currOper, stack<char> &operators)
{
    if (operators.size() == 0)
    {
        operators.push(currOper);
        return;
    }
    if (priorOfOperator(currOper) > priorOfOperator(operators.top()))
    {
        operators.push(currOper);
    }
    else
    {
        cout << operators.top();
        result += operators.top();
        operators.pop();
        return handleStackOperator(result, currOper, operators);
    }
}

//有左括号时直接入栈，然后正常操作，遇到右括号时，将左右括号之间所有运算符出栈
void handleOperator(string &result, char currOper, stack<char> &operators)
{
    if (operators.size() == 0)
    {
        operators.push(currOper);
        return;
    }
    if (currOper == '+' || currOper == '-' || currOper == '*' || currOper == '/')
    {
        handleStackOperator(result, currOper, operators);
    }
    else if (currOper == '(')
    {
        operators.push(currOper);
    }
    else if (currOper == ')')
    {
        while (operators.top() != '(')
        {
            cout << operators.top();
            result += operators.top();
            operators.pop();
        }
        operators.pop();
        return;
    }
}

//计算结果
float caclResult(string &s)
{
    stack<int> result;
    for (int i = 0; i < s.length(); i++)
    {
        if (s[i] >= '0' && s[i] <= '9')
        {
            result.push(s[i] - '0');
        }
        else
        {
            int temp2 = result.top();
            result.pop();
            int temp1 = result.top();
            result.pop();
            if (s[i] == '+')
            {
                result.push(temp1 + temp2);
            }
            else if (s[i] == '-')
            {
                result.push(temp1 - temp2);
            }
            else if (s[i] == '*')
            {
                result.push(temp1 * temp2);
            }
            else if (s[i] == '/')
            {
                result.push(temp1 / temp2);
            }
        }
    }
    return result.top();
}

int main()
{
    ifstream ifs;
    ifs.open("FuckingExpression.txt", ios::in);
    if (!ifs.is_open())
    {
        cout << "打开失败" << endl;
        return 0;
    }
    string test = "";
    ifs >> test; //读入合法的算术表达式
    ifs.close();
    string result = ""; //用来存后缀表达式，便于最后做计算
    cout << test << endl;
    stack<char> test_stack;
    for (int i = 0; i < test.length(); i++)
    {
        if (test[i] >= '0' && test[i] <= '9')
        {
            cout << test[i];
            result += test[i];
        }
        else
        {
            handleOperator(result, test[i], test_stack);
        }
    }
    while (test_stack.size() != 0) //运算符栈不为空则将剩下的全部弹出
    {
        cout << test_stack.top();
        result += test_stack.top();
        test_stack.pop();
    }
    cout << endl;
    cout << caclResult(result) << endl; //计算结果
    return 0;
}